package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Hotel;
import cn.wolfcode.trip.base.domain.HotelContent;
import cn.wolfcode.trip.base.mapper.HotelContentMapper;
import cn.wolfcode.trip.base.mapper.HotelMapper;
import cn.wolfcode.trip.base.query.HotelQueryObject;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IHotelService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class HotelServiceImpl implements IHotelService {
    @Autowired
    private HotelMapper hotelMapper;
    @Autowired
    private HotelContentMapper contentMapper;

    public PageInfo<?> query(HotelQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Hotel> list = hotelMapper.selectAll(qo);
        return new PageInfo<>(list);
    }

    public void saveOrUpdate(Hotel hotel) {
        HotelContent hotelContent = new HotelContent();
        hotelContent.setContent(hotel.getHotelContent().getContent());
        if (hotel.getId() == null){
            hotelMapper.insert(hotel);
            hotelContent.setId(hotel.getId());
            contentMapper.insert(hotelContent);
        }else {
            hotelMapper.updateByPrimaryKey(hotel);
            hotelContent.setId(hotel.getId());
            contentMapper.updateByPrimaryKey(hotelContent);
        }
    }

    public void delete(Long id) {
        hotelMapper.deleteByPrimaryKey(id);
        contentMapper.deleteByPrimaryKey(id);
    }

    public Hotel getById(Long id) {
        return hotelMapper.selectByPrimaryKey(id);
    }
}
